<template>
    <div class="box">
        <div v-cloak class="info" v-show="(!longitude && isShow) || (!latitude && isShow)">
            <div class="flex infotext">
                <strong><i class="icon-map"></i>未获得定位信息</strong>
                <p>请开启定位获取门店福利</p>
            </div>
            <div class="flex open" @click="getlocation()">点此定位</div>
        </div>
        <Welfare :everyDayWelfare="everyDayWelfare" :storeWelfare="storeWelfare"></Welfare>
        <recentActive></recentActive>
    </div>


</template>
<style lang="scss"> 
    @import '../../assets/css/common.scss';        
    @function rem($px){
      @return $px / 10 * 1rem;
    }
    html,body{
        height:100%;
    }
    body {
        background: #f4f4f4;
        margin:0;
    }
    [v-cloak] {
      display: none;
    }
    .icon-map{
       display:inline-block;
       width:rem(13);
       height:rem(15);
       background: url(../../assets/image/map@2x.png) no-repeat 0 0;
       background-size:cover;
       margin-right:rem(6);
       vertical-align:middle;

    }
    .info{

        display: -webkit-flex;
        // flex-direction:column ;
        // flex-wrap:nowrap;
        flex-flow:row nowrap;
        justify-content: space-between;
        align-items:center;
        font-size:rem(15);
        height:rem(70);
        background:#f3f9fc url(../../assets/image/0A179F83-5216-4FED-B562-52F78C553B44@2x.png) no-repeat rem(263) bottom;
        background-size:rem(78/2) rem(58/2);


        .infotext{
            padding-left:rem(15);
            strong{
                color:#676767;
                margin-bottom:rem(4);
            }
            p{padding-left:rem(18);}
        }
        p{
            font-size:rem(13);
            color:#676767;
        }
        .open{
            width:rem(75);
            color:#0076ff;



        }

    }


</style>
<script>
    import common from "../../assets/js/common.js";
    // import Welfare from './storewelfare';
    // import everyDayWelfare from './everydaywelfare';
    // import recentActive from './recentactive';
    import qs from 'querystring';
    import url  from 'url';
    import {Toast} from 'mint-ui';
    export default {
        data(){
            return {
                currentCity: '',
                locationCity: '上海',
                longitude: '',
                latitude: '',
                everyDayWelfare:[],
                storeWelfare:[],
                isShow:false
            };
        },
        created(){
            var _this = this;


			/*_this.$http.post(common.isdev() + '/daily/list/0.htm', {
                'currentCity': '上海市',
                'activityCode': "ATTLB01"
            }).then(function(data){
                var data = _this.parseData(data.data);
                if(data.result === 1){
                    if(data.obj.length > 0){
                       _this.everyDayWelfare = data.obj; 
                    }
                    _this.$nextTick(function(){
		                common.ANA_AnalyticsScan();
		            });
                    //_this.everyDayWelfare = JSON.stringify(data.obj);
                    // console.info(typeof data.obj);
                    // _this.everyDayWelfare = data.obj;
                    //var vm = new Vue()
                    //_this.$broadcast('everyDayWelfare', _this.everyDayWelfare);
                    //vm.$emit('add-todo', 'he');
                    //_this.$refs.mess.mess('1111111');
                    //console.info(_this.$refs.mess);

                }else{
                    Toast('获取数据错误');
                }

            }, function( err) {
                Toast('网络异常,请稍后再试');
            });*/

            //对路径进行解析
            var latLon = this.getParam();
            _this.currentCity = decodeURIComponent(latLon.cityName);
            if(typeof latLon == 'object' && _this.currentCity){
                // _this.currentCity = decodeURIComponent(latLon.cityName);

                lb.geolocation(function(data){
                    var data = _this.parseData(data);
                    _this.isShow = true;
                    if(typeof data.text == 'object'){
                        _this.locationCity = data.text.cityName;
                        _this.longitude = data.text.longitude;
                        _this.latitude = data.text.latitude;
                        console.info('locationCity:'+data.text.cityName);

                        //获取门店列表
                        _this.$http.post(common.isdev() + '/daily/list/1.htm',{
                            'locationCity':_this.locationCity,
                            'currentCity': _this.currentCity,
                            'Y': _this.longitude,
                            'X': _this.latitude,
                            'activityCode': "ATTLB01"
                        }).then(function(data){                
                            var data = _this.parseData(data.data);
                            if(data.result === 1){
                                if(data.obj.length >0 ){
                                   _this.storeWelfare = data.obj; 
                               }
                                
                            }
                            _this.$nextTick(function(){
				                common.ANA_AnalyticsScan();
				            });
                            // else{
                            //     Toast('获取门店券数据错误');
                            // }
                        }, function(err){
                            Toast('网络异常,请稍后再试');
                        });
                    }
                });

            }else{
                Toast('获取当前城市失败');
                return;
            }



            //获取普通券数据 /daily/list/0.htm
            _this.$http.post(common.isdev() + '/daily/list/0.htm', {
                'currentCity': _this.currentCity,
                'activityCode': "ATTLB01"
            }).then(function(data){
                var data = _this.parseData(data.data);
                if(data.result === 1){
                    if(data.obj.length > 0){
                       _this.everyDayWelfare = data.obj; 
                    }
                    _this.$nextTick(function(){
		                common.ANA_AnalyticsScan();
		            });
                    //_this.everyDayWelfare = JSON.stringify(data.obj);
                    // console.info(typeof data.obj);
                    // _this.everyDayWelfare = data.obj;
                    //var vm = new Vue()
                    //_this.$broadcast('everyDayWelfare', _this.everyDayWelfare);
                    //vm.$emit('add-todo', 'he');
                    //_this.$refs.mess.mess('1111111');
                    //console.info(_this.$refs.mess);

                }else{
                    Toast('获取数据错误');
                }

            }, function( err) {
                Toast('网络异常,请稍后再试');
            });



        },
        components:{
            // Welfare,recentActive
            'Welfare': function ( resolve, reject ) {
                require.ensure([], function ( require ) {
                    resolve(require('./storewelfare'));
                }, 'Welfare');
            },
            'recentActive': function ( resolve, reject ) {
                require.ensure([], function ( require ) {
                    resolve(require('./recentActive'));
                }, 'recentActive');
            }
        },
        methods:{
            getParam(){
                var tar = url.parse(window.location.href).query,tmpObj = qs.parse(tar);
                return tmpObj;
            },
            parseData: function(data) {
                var _data;
                if (!data) return data;
                try{
                    _data = JSON.parse(data);
                }catch(e){
                    _data = data;
                }
                return _data;
            },
            getlocation(){
                var _this = this;
                lb.geolocation(function(data){
                    var data = _this.parseData(data);                    
                    if(typeof data.text == 'object'){
                        _this.locationCity = data.text.cityName;
                        _this.longitude = data.text.longitude;
                        _this.latitude = data.text.latitude;
                        window.location.reload();
                    }
                });
            }
        }
    }

</script>